package cn.cocho.custom.yunCashierDf.agency.controller;

import cn.cocho.common.web.app.JsonResultContants;
import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridContants;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.agency.domain.AgencyInfo;
import cn.cocho.custom.yunCashierDf.agency.service.AgencyInfoService;
import cn.cocho.custom.yunCashierDf.common.domain.AreaInfo;
import cn.cocho.custom.yunCashierDf.common.service.CommonService;
import cn.cocho.custom.yunCashierDf.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by WQJ on 2017/5/22.
 */
@Controller
public class AgencyInfoController {

    @Autowired
    private AgencyInfoService agencyInfoService;

    @Autowired
    private CommonService commonService;

    @ResponseBody
    @RequestMapping(value = "/admin/agency/getAgencyInfoList")
    public JQGridPage getAgencyInfoList(JQGridPage<AgencyInfo> pageJQGrid) {
        return agencyInfoService.getListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/agency/editAgencyInfo")
    public void editAgencyInfo(AgencyInfo agencyInfo, HttpServletResponse response) {
        String warnMessage = null;
        if (agencyInfo.getOper().equals(JQGridContants.EDIT_OPER_ADD)) {
            agencyInfoService.add(agencyInfo);
        } else if (agencyInfo.getOper().equals(JQGridContants.EDIT_OPER_UPDATE)) {
            agencyInfo.setAgencyId(agencyInfo.getId());
            agencyInfoService.update(agencyInfo);
        } else if (agencyInfo.getOper().equals(JQGridContants.EDIT_OPER_DEL)) {
            JQGridUtils.delete("delete from agency_info where agency_id ", agencyInfo.getId());
        }
        JQGridUtils.warnUserMessage(warnMessage, response);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/agency/getAgencyInfoById", method = RequestMethod.POST)
    public JsonResult<AgencyInfo> getAgencyInfoById(String agencyId) {
        JsonResult<AgencyInfo> result;
        AgencyInfo agencyInfo = agencyInfoService.getAgencyInfoById(agencyId);
        if (agencyInfo != null) {
            result = new JsonResult<AgencyInfo>(JsonResultContants.SUCCESS, "获取代理机构信息成功！", agencyInfo);
        } else {
            result = new JsonResult<AgencyInfo>(JsonResultContants.FAIL, "无法获取代理机构信息！");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/agency/saveAgencyInfo", method = RequestMethod.POST)
    public JsonResult saveAgencyInfo(AgencyInfo agencyInfo) {
        JsonResult result;
        if (StringUtils.isEmpty(agencyInfo.getAgencyId())) { //新增
            boolean insertResult = agencyInfoService.insertAgencyInfo(agencyInfo);
            if (insertResult == true) {
                result = new JsonResult(JsonResultContants.SUCCESS, "新增代理机构信息成功！", agencyInfo.getAgencyId());
            } else {
                result = new JsonResult(JsonResultContants.FAIL, "新增代理机构信息失败！");
            }
        } else {  //修改
            boolean replaceResult = agencyInfoService.replaceAgencyInfo(agencyInfo);
            if (replaceResult) {
                result = new JsonResult(JsonResultContants.SUCCESS, "修改代理机构信息成功！", agencyInfo.getAgencyId());
            } else {
                result = new JsonResult(JsonResultContants.FAIL, "修改代理机构信息失败！");
            }
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/agency/getAreaProvince")
    public JsonResult getAreaProvince() {
        List<AreaInfo> list = commonService.getAreaProvince();
        JsonResult result = new JsonResult(JsonResultContants.SUCCESS, "省份信息获取成功", list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/agency/getAreaCity")
    public JsonResult getAreaCity(String provinceId) {
        List<AreaInfo> list = commonService.getAreaCity(provinceId);
        JsonResult result = new JsonResult(JsonResultContants.SUCCESS, "城市信息获取成功", list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/agency/getAreaCounty")
    public JsonResult getAreaCounty(String cityId) {
        List<AreaInfo> list = commonService.getAreaCounty(cityId);
        JsonResult result = new JsonResult(JsonResultContants.SUCCESS, "区县信息获取成功", list);
        return result;
    }

}
